/**
 * Created by chesw on 2017/5/19.
 */

var guideConsensus = Vue.extend({
    data: function () {
        return {
            pageInfo: {
                curPageRecord: [],
                keyWord: "",
                category:[],
                totalCount: "",
                pageSize: 10,
                pageCount: 10,
                pageCurrent: 1,
                ImgUrl: "",
                categoryType: ""
            },
            CategoryInfo: [],
            ConditionList: [],
            ConditionListCount:0
        }
    },
    props: [],
    methods: {
        GetGuideConsensusDocuments: function (p_keyWord) {
            var _self = this;
            _self.pageInfo.keyWord = p_keyWord;
            _self.HideEmpty();
            _self.pageInfo.curPageRecord = [];
            _self.$http.post("/MedicalWiki/GetGuideConsensusDocuments", _self.pageInfo).then(function (res) {
                if (res.body.curPageRecord.length > 0) {                   
                    for (var i = 0; i < res.body.curPageRecord.length; i++) {
                        _self.pageInfo.curPageRecord.push(res.body.curPageRecord[i]);
                    }
                    _self.pageInfo.ImgUrl = res.body.ImgUrl;
                    _self.pageInfo.totalCount = res.body.totalCount;
                    _self.pageInfo.pageCount = res.body.pageCount;
                    _self.showPage();
                }
                else {
                    _self.ShowEmpty();
                }
            });
        },
        GetCategories: function () {
            var _self = this;
            var noResult = document.getElementById("no-result");
            _self.pageInfo.categoryType = "guide";
            _self.$http.post("/MedicalWiki/GetCategories", _self.pageInfo).then(function (res) {
                if (res.body.length!=0){
                    _self.CategoryInfo = res.body;
                }
            });
            _self.DisplayCondition(_self.ConditionList.length);
        },
        ShowDetail: function (p_index) {
            var _self = this;
            var param = _self.pageInfo.curPageRecord[p_index];
            _self.$emit('detail', 5, param);
        },
        showPage: function () {
            var _self = this;
            var pageBox = document.getElementById("clinic-page-box");

            if (_self.pageInfo.pageCount === 1)
                pageBox.style.display = "none";
            else
                pageBox.style.display = "block";

            var param = {
                pageCount: _self.pageInfo.pageCount,
                curPage: _self.pageInfo.pageCurrent,
                pageInfo: _self.pageInfo,
                page_func: function (p_curPage, p_pageInfo) {
                    _self.pageInfo.pageCurrent = p_curPage;
                    _self.pageInfo.curPageRecord = [];
                    _self.$http.post("/MedicalWiki/GetGuideConsensusDocuments", _self.pageInfo).then(function (res) {
                        if (res.body.curPageRecord.length > 0) {
                            for (var i = 0; i < res.body.curPageRecord.length; i++) {
                                _self.pageInfo.curPageRecord.push(res.body.curPageRecord[i]);
                            }
                            _self.pageInfo.ImgUrl = res.body.ImgUrl;
                            _self.pageInfo.totalCount = res.body.totalCount;
                            _self.pageInfo.pageCount = res.body.pageCount;
                            _self.showPage();
                        }
                        else {
                            _self.ShowEmpty();
                        }
                    });
                }
            };
            _self.$emit('show', param);
        },
        showTab: function () {
            var _self = this;
            var param = {
                show_tab_func: function () {
                    _self.GetGuideConsensusDocuments();
                }
            };
            _self.$emit('showTab', param);
        },
        detail: function (p_index) {
            var _self = this;
            window.location.href = _self.pageInfo.curPageRecord[p_index].fileSrc;
            window.location.target = "_blank";
        },
        AddConditionList: function (p_parent_index, p_sub_index) {
            var _self = this;
            var categoryExist = false;
            var curCategory = _self.CategoryInfo[p_parent_index].sub_category_list[p_sub_index];
            if (curCategory != null)
                _self.CategoryInfo[p_parent_index].sub_category_list[p_sub_index].isSelected = true;

            for (var i = 0; i < _self.ConditionList.length; i++) {
                if (_self.ConditionList[i].id === curCategory.id) 
                    categoryExist = true;
            }
            
            //如果筛选类别没有选择，则添加这个类别
            if (!categoryExist) {
                //同一个类别的子选项只能出现一次
                for (var i = 0; i < _self.ConditionList.length; i++) {
                    if (_self.ConditionList[i].parentId === curCategory.parent_id) {
                        for (var j = 0; j < _self.CategoryInfo.length; j++) {
                            for (var k = 0; k < _self.CategoryInfo[j].sub_category_list.length; k++) {
                                if (_self.CategoryInfo[j].sub_category_list[k].id === _self.ConditionList[i].id) {
                                    _self.CategoryInfo[j].sub_category_list[k].isSelected = false;
                                    break;
                                }
                            }
                        }
                        _self.ConditionList.pop({ title: _self.ConditionList[i].title, id: _self.ConditionList[i].id, parentId: _self.ConditionList[i].parentId });
                    }
                }

                _self.ConditionList.push({ title: curCategory.title, id: curCategory.id ,parentId:curCategory.parent_id});
                _self.pageInfo.category = _self.ConditionList;
                _self.DisplayCondition(_self.ConditionList.length);
                _self.pageInfo.pageCurrent = 1;
                _self.HideEmpty();
                _self.$http.post("/MedicalWiki/GetGuideConsensusDocuments", _self.pageInfo).then(function (res) {
                    if (res.body.curPageRecord.length > 0) {
                        _self.pageInfo.curPageRecord = [];
                        for (var i = 0; i < res.body.curPageRecord.length; i++) {
                            _self.pageInfo.curPageRecord.push(res.body.curPageRecord[i]);
                        }
                        _self.pageInfo.ImgUrl = res.body.ImgUrl;
                        _self.pageInfo.totalCount = res.body.totalCount;
                        _self.pageInfo.pageCount = res.body.pageCount;
                        _self.showPage();
                    }
                    else {
                        _self.ShowEmpty();
                    }
                });
            }
            
        },
        DeleteCondition: function (p_index) {
            var _self = this;
            for (var i = 0; i < _self.CategoryInfo.length; i++) {
                for (var j = 0; j < _self.CategoryInfo[i].sub_category_list.length; j++) {
                    if (_self.CategoryInfo[i].sub_category_list[j].id == _self.ConditionList[p_index].id) {
                        _self.CategoryInfo[i].sub_category_list[j].isSelected = false;
                    }
                }
            }
            _self.ConditionList.pop(_self.ConditionList[p_index]);
            _self.pageInfo.category = _self.ConditionList;
            _self.DisplayCondition(_self.ConditionList.length);
            _self.pageInfo.pageCurrent = 1;
            _self.HideEmpty();
            _self.$http.post("/MedicalWiki/GetGuideConsensusDocuments", _self.pageInfo).then(function (res) {
                if (res.body.curPageRecord.length > 0) {
                    _self.pageInfo.curPageRecord = [];
                    for (var i = 0; i < res.body.curPageRecord.length; i++) {
                        _self.pageInfo.curPageRecord.push(res.body.curPageRecord[i]);
                    }
                    _self.pageInfo.ImgUrl = res.body.ImgUrl;
                    _self.pageInfo.totalCount = res.body.totalCount;
                    _self.pageInfo.pageCount = res.body.pageCount;
                    _self.showPage();
                }
                else {
                    _self.ShowEmpty();
                }
            });
        },
        closeDetail: function () {
            var caseReport = document.getElementById("case-report");
            caseReport.style.display = "none";
        },
        DisplayCondition: function (p_conditionListCount) {
            var condition = document.getElementById("conditionList");
            if (p_conditionListCount == 0)
                condition.style.display = "none";
            else
                condition.style.display = "block";
        },
        HideEmpty: function () {
            var resultEmpty = document.getElementById("no-result");
            var pageBox = document.getElementById("clinic-page-box");
            pageBox.style.display = "none";
            resultEmpty.style.display = "none";
        },
        ShowEmpty: function (p_show, p_pageObject) {
            var resultEmpty = document.getElementById("no-result");
            var pageBox = document.getElementById("clinic-page-box");
            pageBox.style.display = "none";
            resultEmpty.style.display = "block";
        },
    },
    template: '<div class="guide-consensus">' +
        '<div class="screen-box" >' +
            '<div class="screen-list" v-for="(category, index1) in CategoryInfo">' +
                '<div class="screen-name">{{category.parent_Category_name}}</div>' +
                '<div class="screen-content">' +
                    '<ul class="screen-inner-content">' +
                    '<li  v-for="(item, index2) in category.sub_category_list"><span v-if="index2<10" v-on:click="AddConditionList(index1,index2)" :class="[item.isSelected?\'screenActive\':\'\']">{{item.title}}</span><div class="arrow-btn" v-else>更多</div></li>' +
                    '</ul>' +
                '</div>' +
            '</div>' +
        '</div>' +        
        '<div class="screen-condition" id="conditionList">' +
            '<div class="selected-name">筛选条件：</div>' +
            '<div class="condition-box">' +
                '<a href="javascript:;" class="condition-list" v-for="(condition, index) in ConditionList">' +
                    '<span>{{condition.title}}</span>' +
                    '<i v-on:click="DeleteCondition(index)"></i>' +
                '</a>' +
            '</div>' +
        '</div>' +
        '<ul class="guide-list" v-for=" (item, index) in pageInfo.curPageRecord">' +
            '<li class="guide-detail">' +
                '<p class="content row-overflow">{{item.title}}</p>' +
                '<p class="read-btn"><span><a :href="[item.fileSrc]" target="_blank">在线阅读</a></span></p>' +
            '</li>' +
        '</ul>' +        
    '</div>'
});